﻿# -*- coding:utf-8 -*-
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import User
from homesite.files.models import File, Category



class FileForm(ModelForm): 

    def __init__(self, fcategory, fuser, fname_real, fmimetype, *args, **kwargs):
        super(FileForm, self).__init__(*args, **kwargs)
        self.fuser = fuser
        self.fname_real = fname_real 
        self.fmimetype = fmimetype
        self.fcategory = fcategory
        
    def save(self, commit=True):
        self.cleaned_data['user'] = self.fuser
        self.cleaned_data['name_real'] = self.fname_real
        self.cleaned_data['fmimetype'] = self.fmimetype
        self.cleaned_data['download_cnt'] = 0
        self.cleaned_data['category'] = self.fcategory
        return super(FileForm, self).save(commit)

    name = forms.FileField(widget=forms.FileInput(attrs={'size':'62'}), label='Файл')
    
    class Meta:
        model = File 
        exclude = ('user', 'name_real', 'mimetype', 'download_cnt', 'category')
        
        
        
class FileEditForm(ModelForm):         

    category =  forms.ModelChoiceField(Category.objects.all(), label='Категория')
    title = forms.CharField(max_length=64, widget=forms.TextInput(attrs={'size':'80'}), label='Название')
    description = forms.CharField(max_length=128, widget=forms.Textarea, required=False, label='Описание') 

 

class CategoryCreateForm(ModelForm):    

    def __init__(self, fparent, *args, **kwargs):
        super(CategoryCreateForm, self).__init__(*args, **kwargs)
        self.fparent = fparent
        
    def save(self, commit=True):
        self.cleaned_data['parent'] = self.fparent 
        self.cleaned_data['files_count'] = 0  
        return super(CategoryCreateForm, self).save(commit)
     
    class Meta:
        model = Category
        exclude = ('parent', 'files_count')



class CategoryEditForm(ModelForm):        
  
    title = forms.CharField(max_length=32, widget=forms.TextInput(attrs={'size':'80'}), label='Название')

    def save(self, commit=True):
        self.cleaned_data['files_count'] = 0  
        return super(CategoryEditForm, self).save(commit)

    class Meta:
        model = Category    
        exclude = ('files_count')
        